<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/26
 * Time: 17:18
 */

namespace app\admin\controller\member;


use app\admin\model\User;
use think\Controller;
use think\Session;

class Table extends Controller
{
    public function page(){
        $user=new User();
        $userInfo=(new Session())->get("user");
        if($userInfo["scope"]==0){
            $items=$user
                ->where([
                    "is_del"        =>0,
                    "uid"      =>$userInfo["id"]
                ])
                ->paginate(15);
        }else{
            $items=$user
                ->where([
                    "is_del"        =>0,
                ])
                ->paginate(15);
        }
        $page=$items->render();
        $this->assign("items",$items);
        $this->assign("page",$page);
        return $this->fetch("index");
    }

    public function edit(){
        $user=new User();
        if($this->request->isGet()){
            if($this->request->has("id")){//编辑
                $id=$this->request->get("id");
                $item=$user->find([
                    "uid"    =>$id
                ]);
                $this->assign("item",$item);
            }else{//添加

            }
            return $this->fetch();
        }else{
            $data=$this->request->param();
            //dump($data);die();
            if(!empty($data["uid"])){//更新
                $user::update($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"update success"
                ]);
            }else{//插入
                unset($data["uid"]);
                $user->save($data);
                return json([
                    "code"      =>200,
                    "msg"       =>"insert success"
                ]);
            }
        }

    }

    public function del(){
        $id=$this->request->param("id");
        $user=new User();
        $res=$user::update([
            "uid"        =>$id,
            "is_del"    =>1
        ]);
        if($res){
            return json([
                "code"      =>200,
                "msg"       =>"success"
            ]);
        }
    }
}